<?php

require_once "smarty/SmartyValidate.class.php";
require_once "ACBController.php";

/**
 * Description of OrderController
 *
 * @author Admin
 */
class OrderController extends ACBController
{
    function __construct()
    {
        parent::__construct();
    }
    
    function initPage($inEnglish = false)
    {
        if(empty($_POST)) //No input --> page opened for first time
        {
            SmartyValidate::connect($this->acb, true);
            //Register validators
            SmartyValidate::register_criteria('validPhoneNumber','valid_phone_number');
            SmartyValidate::register_validator('fname','name','notEmpty');
            SmartyValidate::register_validator('fdescription','description','notEmpty');
            SmartyValidate::register_validator('femail','email','isEmail');
            SmartyValidate::register_validator('fphonenumber','phoneNumber','validPhoneNumber');
            $this->acb->assign('name', "");
            $this->acb->assign('email', "");
            $this->acb->assign('function', "");
            $this->acb->assign('companyName', "");
            $this->acb->assign('phoneNumber', "");
            $this->acb->assign('description', "");
            if ($inEnglish)
                $this->display('order_EN.tpl');
            else
                $this->display('order.tpl');
        }
        else
        {
            SmartyValidate::connect($this->acb);
            if(SmartyValidate::is_valid($_POST)) 
            {
                SmartyValidate::disconnect();
                $this->sendEmail();
                header( "refresh:5;url=index.php" );
                if ($inEnglish)
                    echo "Your request has been sent, you will be redirected to acbmediation.nl in just a few seconds";
                else
                    echo "Uw verzoek is verstuurd, u wordt over enkele momenten teruggeleid naar acbmediation.nl";
            } 
            else 
            {
                // error, redraw the form
                $this->acb->assign($_POST);
                if ($inEnglish)
                    $this->display('order_EN.tpl');
                else
                    $this->display('order.tpl');
            }
        }
    }
    
    function sendEmail()
    {
        $to = "manonschonewille@home.nl";
        $subject = "Verzoek via acbmediation.nl";
        $message = "Algemene informatie\nNaam: " . $_POST["name"] . "\n"
                . "Functie: " . $_POST["function"] . "\n"
                . "Bedrijfsnaam: " . $_POST["companyName"] . "\n"
                . "Telefoonnummer: " . $_POST["phoneNumber"] . "\n\n"
                . "Verzoek\n" . $_POST["description"];
        $headers = "From:" . "orders@acbmediation.nl" . "\r\n" . "Reply-To:" . $_POST["email"];
        mail($to,$subject,$message,$headers);
    }
}

function valid_phone_number($value, $empty, &$params, &$formvars) 
{
    if (empty($formvars))
        return true;
    if (empty($formvars['phoneNumber']) || strlen($formvars['phoneNumber']) < 10 || strlen($formvars['phoneNumber']) > 15)
        return false;
    $phoneNumber = $formvars['phoneNumber'];
    if ($phoneNumber[0] == '+')
        $phoneNumber = substr ($phoneNumber, 1);
    $phoneNumber = str_replace("-", "", $phoneNumber);
    return (is_numeric($phoneNumber));
}

?>
